/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.ui.sdk;

import org.eclipse.core.runtime.IProgressMonitor;
import org.python.pydev.core.ICodeCompletionASTManager;
import org.python.pydev.core.IInterpreterManager;
import org.python.pydev.core.IModule;
import org.python.pydev.plugin.nature.PythonNature;
import org.python.pydev.ui.interpreters.IInterpreterObserver;

/**
 * Observer used to rebuild the AST for completions if the "google" module cannot
 * be resolved.  This is done in the case that the path contribution made by the
 * SDK path contributor did not make it into the AST manager.
 */
public class ZGaeSdkPathInterpreterObserver implements IInterpreterObserver
{
    /**
     * @see org.python.pydev.ui.interpreters.IInterpreterObserver#notifyDefaultPythonpathRestored(org.python.pydev.core.IInterpreterManager, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
     */
    public void notifyDefaultPythonpathRestored(IInterpreterManager aManager, String defaultSelectedInterpreter, IProgressMonitor monitor)
    {
    }

    /**
     * @see org.python.pydev.ui.interpreters.IInterpreterObserver#notifyInterpreterManagerRecreated(org.python.pydev.core.IInterpreterManager)
     */
    public void notifyInterpreterManagerRecreated(IInterpreterManager interpreterManager)
    {
    }

    /**
     * @see org.python.pydev.ui.interpreters.IInterpreterObserver#notifyProjectPythonpathRestored(org.python.pydev.plugin.nature.PythonNature, org.eclipse.core.runtime.IProgressMonitor, java.lang.String)
     */
    public void notifyProjectPythonpathRestored(PythonNature nature, IProgressMonitor monitor, String defaultSelectedInterpreter)
    {
    }

    /**
     * @see org.python.pydev.ui.interpreters.IInterpreterObserver#notifyNatureRecreated(org.python.pydev.plugin.nature.PythonNature, org.eclipse.core.runtime.IProgressMonitor)
     */
    public void notifyNatureRecreated(PythonNature nature, IProgressMonitor monitor)
    {
        // If the google module can't be resolved, then the SDK is not on
        // the project's PYTHONPATH yet.  Our contributor would have added it
        // so the AST manager must have been created before the contributor
        // contributed the SDK to the path.  Simply rebuild the path and we 
        // should be good.
        ICodeCompletionASTManager astManager = nature.getAstManager();
        IModule module = astManager.getModule("google", nature, false);
        if (module == null)
        {
            nature.rebuildPath(null, monitor);
        }
    }
}
